<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <my-bread>素材管理</my-bread>
      </div>
      <!-- 按钮 -->
      <div style="margin-top: 20px">
        <el-radio-group
          v-model="form.collect"
          @change="toggleCollect"
          size="small"
        >
          <el-radio-button :label="false">全部</el-radio-button>

          <el-radio-button :label="true">收藏</el-radio-button>
        </el-radio-group>
        <el-button
          style="float: right"
          type="success"
          size="small"
          @click="openDialog"
          >添加素材</el-button
        >
        <!-- {{form.collect}} -->
      </div>

      <!-- 表 -->
      <!-- 图片列表 -->
      <ul class="img-list">
        <li class="img-item" v-for="item in images" :key="item.id">
          <img :src="item.url" alt="" />
          <p class="action" v-if="!form.collect">
            <span
              class="el-icon-star-off"
              @click="changeCollectt(item)"
              :class="{ active: item.is_collected }"
            ></span>
            <span class="el-icon-delete" @click="deleteImage(item.id)"></span>
          </p>
        </li>
      </ul>
      <!-- 分页 -->
      <!-- 分页组件 -->
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="form.page"
        :page-size="form.per_page"
        @current-change="toggle"
      >
      </el-pagination>
    </el-card>
    <!-- duihuakuang -->
    <el-dialog title="添加素材" :visible.sync="dialogVisible" width="400px" append-to-body>
      <el-upload
        class="avatar-uploader"
        :action="uploadURL"
        :show-file-list="false"
         :on-success="handleUploadSuccess"
         :headers="uploadHeaders"
         name="image"
      >
      <!-- <span>上传组件</span> -->
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // radio3: "上海",
      form: {
        collect: false,
        page: 1,
        per_page: 12

      },
      uploadHeaders: {
        Authorization: 'Bearer ' + window.localStorage.getItem('token')
      },
      images: [],
      total: 0,
      dialogVisible: false,
      imageUrl: '',
      uploadURL: this.$http.defaults.baseURL + '/mp/v1_0/user/images'
    }
  },
  created () {
    console.dir(this.$http)
    this.form.page = 1
    this.getPics()
    // console.dir(this.$http);
  },
  methods: {
    handleUploadSuccess (result) {
      console.log(result)
      this.imageUrl = result.data.url
      this.$message({
        type: 'success',
        message: '上传成功!'
      })
      window.setTimeout(() => {
        this.dialogVisible = false
      }, 3000)
      this.getPics()
    },
    openDialog () {
      this.imageUrl = ''
      this.dialogVisible = true
    },
    deleteImage (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          try {
            this.$http.delete('/mp/v1_0/user/images/' + id)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getPics()
            // console.log(1111111);
          } catch {
            (e) => {
              this.$message({
                type: 'error',
                message: '删除失败!'
              })
            }
          }
        })
        .catch(() => {})
    },
    // async changeCollectt(item) {

    //  try{const {data:{data}} = await this.$http.put('/mp/v1_0/user/images/'+item.id,{collect:!item.is_collected});
    //   this.$message.successs(data.collect?'收藏操作成功':'取消操作成功')
    //   item.is_collected = data.collect}catch{e=>{ this.$message.error('失败')}}
    // },
    async changeCollectt (item) {
      console.log(item)
      try {
        // collect 你想修改的素材状态，和当前item.is_collected状态取反即可
        const {
          data: { data }
        } = await this.$http.put(`/mp/v1_0/user/images/${item.id}`, {
          collect: !item.is_collected
        })
        // 操作成功
        this.$message.success(data.collect ? '添加收藏成功' : '取消收藏成功')
        // 通过修改数据去驱动星星的颜色
        item.is_collected = data.collect
      } catch (e) {
        this.$message.error('操作失败')
      }
    },
    toggleCollect () {
      this.getPics()
    },
    async getPics () {
      const {
        data: { data }
      } = await this.$http.get('/mp/v1_0/user/images', { params: this.form })
      this.total = data.total_count
      this.images = data.results
    },
    toggle (changedPage) {
      this.form.page = changedPage
      this.getPics()
    }
  }
}
</script>

<style scoped lang='less'>
.img-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  .img-item {
    width: 180px;
    height: 180px;
    margin-right: 60px;
    margin-bottom: 25px;
    border: 1px dashed #ddd;
    position: relative;
    img {
      width: 100%;
      height: 100%;
    }
    .action {
      margin: 0;
      position: absolute;
      left: 0;
      bottom: 0;
      height: 30px;
      line-height: 30px;
      width: 100%;
      background: rgba(0, 0, 0, 0.3);
      color: #fff;
      text-align: center;
      span {
        margin: 0 20px;
        &.active {
          color: red;
        }
      }
    }
  }
}

</style>
